<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :xl="6" :lg="6" :md="6" :sm="24">
            <a-form-item label="面料编号">
              <a-input placeholder="请输入面料编号" v-model="queryParam.fabricCode" style="width: 100%"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="6" :md="6" :sm="24">
            <a-form-item label="供应商编号">
              <a-input placeholder="请输入供应商编号" v-model="queryParam.gysCode" style="width: 100%"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="6" :md="6" :sm="24">
            <a-form-item label="销售状态">
              <a-select placeholder="请选择销售状态" style="width: 100%" v-model="queryParam.status">
                <a-select-option value="0">
                  上架中
                </a-select-option>
                <a-select-option value="2">
                  已下架
                </a-select-option>
              </a-select>
            </a-form-item>

          </a-col>

        </a-row>

        <a-row>
          <a-col :xl="6" :lg="6" :md="6" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
                <a-button type="primary" @click="showModal" style="margin-left: 8px">添加主推面料</a-button>
              <a-button :href="exportFabric" style="margin-left: 8px" type="primary" icon="export">下载模板</a-button>

              <a-button :href="exportFabric2" style="margin-left: 8px" type="primary"
                        icon="export">导出</a-button>
              <a-upload style="margin-left: 8px" name="file" :showUploadList="false"
                        :multiple="false"
                        :customRequest="myupload"
                        :action="importExcelUrl" @change="handleImportExcel">
                <a-button :loading="ltag" type="primary" icon="import"><upload-outlined/>导入</a-button>
              </a-upload>
            </span>
          </a-col>
        </a-row>

        <!--新增主推面料弹框-->
        <a-modal
          title="新增主推面料"
          v-model:visible="visible2"
          @ok="addFirst"
          @close="cleatDate"
        >
          <a-form>
            <a-form-item label="面料编号">
              <a-input v-model="fabricCode" placeholder="请输入面料编号"></a-input>
            </a-form-item>
            <a-form-item label="活动价格">
              <a-input-number style="width: 100%" v-model="price" placeholder="请输入活动价格" suffix="元"></a-input-number>
            </a-form-item>
            <a-form-item label="备注信息">
              <a-textarea v-model="remark" placeholder="备注" :rows="4"></a-textarea>
            </a-form-item>
          </a-form>
        </a-modal>
        <!--新增主推面料弹框结束-->

      </a-form>
    </div>
    <!-- 查询区域-END -->

    <a-modal
      title="编辑主推面料"
      v-model:visible="upflag"
      @ok="updatePPrice"
      @close="cleatDate"
    >
      <a-form>
        <a-form-item :required="true" label="主推价格">
          <a-input-number v-model="firprice" style="width: 100%" placeholder="零剪价(元/米)"
                          suffix="元"></a-input-number>
        </a-form-item>
        <a-form-item label="备注信息">
          <a-textarea v-model="firremake" placeholder="备注" :rows="4"></a-textarea>
        </a-form-item>
      </a-form>
    </a-modal>

    <!--table区域-begin-->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>

        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="30px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a-menu-item style="display: inline;margin-left: 10px">
            <a @click="showDrawer(record),getFabricDetail(record.id)">查看详情</a>
          </a-menu-item>
          <a-menu-item style="display: inline;margin-left: 10px">
<!--            <a @click="gotoUpdate(record.id)">编辑</a>-->
            <a @click="openup(record)">编辑</a>
          </a-menu-item>
          <a-menu-item style="display: inline;margin-left: 10px">
                  <a-popconfirm title="确定删除吗?" @confirm="() => fabricDel(record.id)">
                    <a style="color:deepskyblue">删除</a>
                  </a-popconfirm>
          </a-menu-item>
          <!--          <a-menu-item v-if="record.status=='0'" style="display: inline;margin-left: 10px">
                            <a-popconfirm title="确定下架吗?" @confirm="() => FabricSer(record.id)">
                              <a style="color:deepskyblue">下架</a>
                            </a-popconfirm>
                    </a-menu-item>-->

          <!--          <a-menu-item v-if="record.status=='2'" style="display: inline;margin-left: 10px">
                            <a-popconfirm title="确定上架吗?" @confirm="() => fabricIDFunc(record.id)">
                              <a style="color:deepskyblue">上架</a>
                            </a-popconfirm>
                    </a-menu-item>-->
        </span>
      </a-table>
    </div>

    <!--详情的弹框-->
    <div>
      <a-drawer
        title="面料基本信息"
        placement="right"
        :closable="false"
        :visible="visible"
        :after-visible-change="afterVisibleChange"
        :width="648"
        @close="onClose"
      >
        <!--上方的订单基础信息-->
        <div style="margin-top: 10px;width: 100%;margin-bottom: 30px;">
          <table>
            <tr class="mytr">
              <td class="fatitle">面料编号</td>
              <td class="facontent">{{ fabricDetail.fabricCode }}</td>
              <td class="fatitle">面料类型</td>
              <td v-if="fabricDetail.fabricTypeId==1" class="facontent">素色</td>
              <td v-if="fabricDetail.fabricTypeId==2" class="facontent">条纹</td>
              <td v-if="fabricDetail.fabricTypeId==3" class="facontent">格子</td>
            </tr>
            <tr class="mytr">
              <td class="fatitle">供应商面料编号</td>
              <td class="facontent">{{ fabricDetail.gysfabriccode }}</td>
              <td class="fatitle">供应商</td>
              <td class="facontent">{{ fabricDetail.gysname }}</td>
            </tr>
            <tr class="mytr">
              <td class="fatitle">是否进口</td>
              <td class="facontent">{{ fabricDetail.isimport == '0' ? '国产' : '进口' }}</td>
              <td class="fatitle">面料产地</td>
              <td class="facontent">{{ fabricDetail.fabricSource }}</td>
            </tr>
            <tr class="mytr">
              <td class="fatitle">面料品牌</td>
              <td class="facontent">{{ fabricDetail.pingpai }}</td>
              <td class="fatitle">面料成分</td>
              <td class="facontent">{{ fabricDetail.fabricContent }}</td>
            </tr>

            <tr class="mytr">
              <td class="fatitle">面料纱织</td>
              <td class="facontent">{{ fabricDetail.fabricShazhi }}</td>
              <td class="fatitle">面料克重</td>
              <td class="facontent">{{ fabricDetail.fabricWeight }}</td>
            </tr>

            <tr class="mytr">
              <td class="fatitle">面料门幅</td>
              <td class="facontent">{{ fabricDetail.menfu }}</td>
              <td class="fatitle">面料颜色</td>
              <td class="facontent">{{ fabricDetail.fabriceColor }}</td>
            </tr>

            <tr class="mytr">
              <td class="fatitle">面料花型</td>
              <td class="facontent">{{ fabricDetail.hua }}</td>
              <td class="fatitle">面料季节</td>
              <td class="facontent">{{ fabricDetail.season }}</td>
            </tr>

            <tr class="mytr">
              <td class="fatitle">库存</td>
              <td class="facontent">{{ fabricDetail.inventory }}</td>
              <td class="fatitle">排序</td>
              <td class="facontent">{{ fabricDetail.paixu }}</td>
            </tr>
          </table>
        </div>

        <!--弹框要显示的内容,遍历面料-->
        <div style="left: 10px;margin-bottom: 20px;">

          <div style="margin-left: 10px">
            <!--左边的图片-->
            <div style="float: left">
              <img style="width: 153px;height: 158px;left: 10px;margin-top: 10px" :src="fabricDetail.fabricImg">
              </img>
            </div>

            <div>
              <table style="width: 100%">

              </table>
            </div>
          </div>
        </div>
        <div>面料备注信息：</div>
        <span>{{ fabricDetail.remark }}</span>
      </a-drawer>
    </div>
    <!--弹框结束-->
  </a-card>

</template>

<script>
import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import JDate from '@/components/jeecg/JDate.vue'
import {getAction, postAction} from "../../api/manage";

export default {
  name: 'FirstFabric',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDate
    // LgyxCreditRateModal
  },
  data() {
    return {
      firprice: '',
      firremake: '',
      fircode: '',
      upflag: false,
      visible: false, // 右侧详情弹框的显示
      visible2: false, // 新增弹框的显示
      description: '面料订单',
      fabricDetail: {}, // 面料详情,
      labelCol: {span: 4},
      wrapperCol: {span: 14},
      fabricCode: '',//面料编码
      price: '',// 价格
      remark: '', // 备注
      /*fabricOrderDetail: [], // 当前的面料订单详情
      orderDetail: {}, // 订单详情*/
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '面料编号',
          align: "center",
          dataIndex: 'fabricCode'
        },
        /*{
          title: '供应商编号',
          align: "center",
          dataIndex: 'orgCode'
          /!* customRender: (text) => {
             //字典值替换通用方法
             return filterDictTextByCache('sex', text);
           }*!/
        },*/
        {
          title: '供应商',
          align: 'center',
          dataIndex: 'username'
        },
        {
          title: '面料缩略图',
          align: "center",
          dataIndex: 'fabricImg',
          scopedSlots: {customRender: 'imgSlot'}
        },
        {
          title: '零售价格（元/米）',
          align: "center",
          dataIndex: 'price'
        },
        {
          title: '库存（米）',
          align: "center",
          dataIndex: 'inventory'
        },
        {
          title: '品牌',
          align: "center",
          dataIndex: 'pingpai'
        },
        /*{
          title: '面料组',
          align: "center",
          dataIndex: 'groupName'
        },*/
        {
          title: '销售状态',
          align: "center",
          dataIndex: 'status',
          customRender: function (text) {
            if (text == '0') {
              return '上架中'
            } else if (text == '2') {
              return '已下架'
            }
          }
        },
        {
          title: '备注信息',
          align: 'center',
          dataIndex: 'remark'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          // fixed:"right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: '/fabricCenter/getFirstFabricList',// 面料列表
        delFabric: '/fabricCenter/delFirstFabric',//删除面料
        serFabric: '/fabricCenter/serFabric', //下架面料
        fanricDetail: '/fabricCenter/getFabricDetail', //面料详情
        addFirstFabric: '/fabricCenter/addFirstFabric', //添加主推面料
        fabricUp: '/fabricCenter/fabricUp', //上架面料
        exportFabric: "fabricCenter/exportFabric", // 下载模板
        exportFabric2: "help/exportFirstFabric", // 导出Excel
        importExcelUrl: "fabricCenter/importFirstFabricExcel", // 导入主推面料
      },
      dictOptions: {},
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },

    exportFabric: function () {
      return `${window._CONFIG['domianURL']}/${this.url.exportFabric}`
    },

    exportFabric2: function () {
      return `${window._CONFIG['domianURL']}/${this.url.exportFabric2}`
    }
  },
  methods: {

    // 导入面料excel
    myupload(data) {
      this.ltag = true
      let fdata = new FormData()
      fdata.append('file', data.file)
      fdata.append('group', this.$route.query.group)
      postAction(this.url.importExcelUrl, fdata).then(res => {
        if (res.code == '0') {
          this.ltag = false
          this.$message.success("导入成功")
          this.loadData()
        } else {
          this.ltag = false
          this.$message.error(res.message)
        }
      })
    },

    /**
     * 更新主推面料价格
     */
    updatePPrice() {
      if (this.fircode == '' || this.fircode == null) {
        this.$message.warning("请选择面料")
        return
      } else if (this.firprice == '' || this.firprice == null) {
        this.$message.warning("请输入面料价格")
        return;
      } else {
        getAction(this.url.addFirstFabric, {
          'fabricCode': this.fircode,
          'price': this.firprice,
          'remark': this.firremake
        }).then(res => {
          if (res.code == '200') {
            this.upflag = false
            this.$message.success('操作成功')
            this.loadData()
          } else {
            this.$message.warning('操作失败')
          }
        })
      }
    },

    /**
     * 打开编辑弹框
     */
    openup(data) {
      this.upflag = true
      this.firprice = data.price
      this.firremake = data.remark
      this.fircode = data.fabricCode
    },

    /**
     * 面料上架
     */
    fabricIDFunc(fabricID) {
      getAction(this.url.fabricUp, {
        'fabricID': fabricID
      }).then(res => {
        if (res.code == '200') {
          this.$message.success("上架成功")
          this.loadData()
        } else {
          this.$message.error("上架失败")
        }
      })
    },
    addFirst() { // 添加主推面料
      if (this.fabricCode == '' || this.fabricCode == null) {
        this.$message.warning("请输入面料编号")
        return
      } else if (this.price == '' || this.price == null) {
        this.$message.warning("请输入面料价格")
        return;
      } else {
        getAction(this.url.addFirstFabric, {
          'fabricCode': this.fabricCode,
          'price': this.price,
          'remark': this.remark
        }).then(res => {
          if (res.code == '200') {
            this.visible2 = false
            this.$message.success(res.message)
            this.loadData()
          } else {
            this.$message.warning(res.message)
          }
          this.cleatDate()
        })
      }
    },
    gotoUpdate(fabricID) { // 前往编辑面料界面
      this.$router.push({
        path: '/fabric/UpdateFabric',
        query: {
          fabric: fabricID
        }
      })
    },
    showModal() { // 新增主推面料弹框现实
      // this.cleatDate()
      this.visible2 = true;
    },
    // 获取面料详情
    getFabricDetail(id) {
      getAction(this.url.fanricDetail,
        {'fabricID': id}
      ).then(res => {
        this.fabricDetail = res.result
      })
    },
    // 下面三个是详情弹框的方法
    afterVisibleChange(val) {
      console.log('visible', val);
    },
    showDrawer(record) {
      this.orderSn = record.orderSn
      this.orderDetail = record // 订单详情
      this.visible = true;
    },
    onClose() {
      this.visible = false;
      this.fabricOrderDetail = []
    },

    gotoFabricList(groupID) { // 页面跳转
      this.$router.push({
        path: '/fabric/fabricList',
        query: {
          group: groupID
        }
      })
    },

    fabricDel(fabricID) { // 面料删除
      getAction(this.url.delFabric,
        {
          'id': fabricID
        }).then(res => {
        this.cleatDate()
        if (res.code == '200') {
          this.$message.success("删除成功")
          this.loadData()
        } else {
          this.$message.error("删除失败")
        }
      })
    },

    FabricSer(fabricID) { // 面料下架
      getAction(this.url.serFabric,
        {
          'fabricID': fabricID
        }).then(res => {
        this.cleatDate()
        if (res.code == '200') {
          this.$message.success("下架成功")
          this.loadData()
        } else {
          this.$message.error("下架失败")
        }
      })
    },
    cleatDate() { // 添加组之后清楚数据
      this.fabricCode = ''
      this.price = ''
      this.remark = ''
      // this.visible2 = false
    },
    initDictConfig() {
    }
  }
}
</script>

<style scoped>
@import '~@assets/less/common.less';

.fabricName {
  margin-left: 10px;
  margin-top: 10px;
  width: auto;
  display: block !important;
  background: #2d2d2d;
  color: #ffe56d;
  text-align: center;
  border-radius: 3px;
}

.fabigtitle {
  height: 34px;
  width: 200px;
  text-align: left;
}

.fabigcontent {
  height: 34px;
  width: 200px;
  text-align: right;
}

.fatitle {
  height: 34px;
  width: 82px;
  background: rgb(242 242 242);
  text-align: center;
}

.facontent {
  width: 180px;
  overflow: hidden;
  text-align: center;
}
</style>